package com.kzw.oa.action.info;


import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.kzw.core.orm.Page;
import com.kzw.core.orm.StringPropertyFilter;
import com.kzw.core.util.ContextUtil;
import com.kzw.core.util.ExtjsUtil;
import com.kzw.core.web.action.BaseAction;
import com.kzw.core.web.paging.PagingBean;
import com.kzw.oa.model.info.InMessage;
import com.kzw.oa.model.info.ShortMessage;
import com.kzw.oa.model.system.AppUser;
import com.kzw.oa.service.info.InMessageService;
import com.kzw.oa.service.info.MessageService;
import com.kzw.oa.service.info.ShortMessageService;
import com.kzw.oa.service.system.AppUserService;
import com.kzw.oa.util.JSON;

/**
 * 短消息
 * */
public class ShortMessageAction extends BaseAction {

	static short NOT_DELETE=(short)0;
	private ShortMessage shortMessage;
    private Date from;
    private Date to;
	private List<InMessage> inList=new ArrayList<InMessage>();
	
	@Resource
	private ShortMessageService shortMessageService;
	@Resource
	private InMessageService inMessageService;
	@Resource
	private AppUserService appUserService;
	@Autowired
	private MessageService messageService;
	
	
	public List<InMessage> getInList() {
		return inList;
	}

	public void setInList(List<InMessage> inList) {
		this.inList = inList;
	}

	public Date getFrom() {
		return from;
	}

	public void setFrom(Date from) {
		this.from = from;
	}

	public Date getTo() {
		return to;
	}

	public void setTo(Date to) {
		this.to = to;
	}

	public ShortMessage getShortMessage() {
		return shortMessage;
	}

	public void setShortMessage(ShortMessage shortMessage) {
		this.shortMessage = shortMessage;
	}
	
	
	
	/**
	 * 发送列表
	 */
	public String list() {
		HttpServletRequest request = getRequest();
		List<StringPropertyFilter> filters = ExtjsUtil.getFilters(request);
		// 收件人是我
		filters.add(new StringPropertyFilter("EQL_senderId", ContextUtil.getCurrentUserId()+""));
		filters.add(new StringPropertyFilter("EQH_delFlag", "0"));	//delFlag不等于1
		Page<ShortMessage> page = messageService.search2(ExtjsUtil.getPageReq(request), filters);

		jsonString = new JSON(page).buildWithFilters(3);
		return SUCCESS;
	}
	
	/**
	 * 发送
	 * @return
	 */
	public String send(){
		String reIds = getRequest().getParameter("userId");
		String userFullnames = getRequest().getParameter("userFullname");
		
		String content=getRequest().getParameter("content");
		
		AppUser appUser=ContextUtil.getCurrentUser();
		if(StringUtils.isNotEmpty(reIds)&&StringUtils.isNotEmpty(content)){
			String st[]=reIds.split(",");
			ShortMessage message=new ShortMessage();
			message.setContent(content);
			message.setMsgType((short)1);
			message.setSenderId(appUser.getUserId());
			message.setSender(appUser.getFullname());
			message.setSendTime(new Date());
			message.setUserIds(reIds);
			message.setUserFullnames(userFullnames);
			
			shortMessageService.save(message);
			for(int i=0;i<st.length;i++){
				InMessage in=new InMessage();	
				in.setUserId(Long.parseLong(st[i]));
				AppUser user=appUserService.get(Long.parseLong(st[i]));
				in.setUserFullname(user.getFullname());
				in.setDelFlag(NOT_DELETE);
				in.setReadFlag((short)0);
				in.setShortMessage(message);
				inMessageService.save(in);
			}						
			setJsonString("{success:true}");
		}else{
			setJsonString("{success:false}");
		}
		return SUCCESS;
	}
	
	/**
	 * 批量删除
	 */
	public String multiDel() {
		String[] ids = getRequest().getParameterValues("ids");
		messageService.removeShortMessage(ids);
		jsonString = "{success:true}";
		return SUCCESS;
	}
	

}
